﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace NovoSerDes
{
    public partial class CusUIUintUpDown : UIPanel, IToolTip
    {
        public delegate void OnValueChanged(object sender, decimal value);

        private uint _value;

        private int step = 1;

        private readonly CusUIEdit edit = new CusUIEdit();

        private Color pnlColor;

        private int buttonWidth = 29;

        private IContainer components;

        private UISymbolButton btnDec;

        private UISymbolButton btnAdd;

        private UIPanel pnlValue;

        [DefaultValue(0)]
        [Description("选中数值")]
        [Category("SunnyUI")]
        public uint Value //
        {
            get
            {
                return _value;
            }
            set
            {
                value = (uint)edit.CheckMaxMin(value); //
                if (_value != value)
                {
                    _value = value;
                    pnlValue.Text = _value.ToString();
                    this.ValueChanged?.Invoke(this, _value);
                }
            }
        }

        [DefaultValue(1)]
        [Description("步进值")]
        [Category("SunnyUI")]
        public int Step
        {
            get
            {
                return step;
            }
            set
            {
                step = Math.Max(1, value);
            }
        }

        [Description("最大值")]
        [Category("SunnyUI")]
        [DefaultValue(int.MaxValue)]
        public decimal Maximum //
        {
            get
            {
                return (uint)edit.MaxValue; //
            }
            set
            {
                edit.MaxValue = (uint)value; //
            }
        }

        [Description("最小值")]
        [Category("SunnyUI")]
        [DefaultValue(int.MinValue)]
        public int Minimum
        {
            get
            {
                return (int)edit.MinValue;
            }
            set
            {
                edit.MinValue = value;
            }
        }

        [DefaultValue(true)]
        [Description("是否可以双击输入")]
        [Category("SunnyUI")]
        public bool Inputable { get; set; } = true;


        [DefaultValue(false)]
        [Description("是否只读")]
        [Category("SunnyUI")]
        public bool ReadOnly { get; set; }

        [Description("填充颜色")]
        [Category("SunnyUI")]
        [DefaultValue(typeof(Color), "80, 160, 255")]
        public Color ButtonFillColor
        {
            get
            {
                return btnAdd.FillColor;
            }
            set
            {
                UISymbolButton uISymbolButton = btnDec;
                Color color2 = (btnAdd.FillColor = value);
                uISymbolButton.FillColor = color2;
            }
        }

        [DefaultValue(typeof(Color), "115, 179, 255")]
        [Category("SunnyUI")]
        [Description("鼠标移上时填充颜色")]
        public Color ButtonFillHoverColor
        {
            get
            {
                return btnAdd.FillHoverColor;
            }
            set
            {
                UISymbolButton uISymbolButton = btnDec;
                UISymbolButton uISymbolButton2 = btnAdd;
                UISymbolButton uISymbolButton3 = btnDec;
                Color color2 = (btnAdd.FillHoverColor = value);
                Color color4 = (uISymbolButton3.FillHoverColor = color2);
                Color rectHoverColor = (uISymbolButton2.RectHoverColor = color4);
                uISymbolButton.RectHoverColor = rectHoverColor;
            }
        }

        [DefaultValue(typeof(Color), "64, 128, 204")]
        [Category("SunnyUI")]
        [Description("鼠标按下时填充颜色")]
        public Color ButtonFillPressColor
        {
            get
            {
                return btnAdd.FillPressColor;
            }
            set
            {
                UISymbolButton uISymbolButton = btnDec;
                UISymbolButton uISymbolButton2 = btnAdd;
                UISymbolButton uISymbolButton3 = btnDec;
                Color color2 = (btnAdd.FillPressColor = value);
                Color color4 = (uISymbolButton3.FillPressColor = color2);
                Color rectPressColor = (uISymbolButton2.RectPressColor = color4);
                uISymbolButton.RectPressColor = rectPressColor;
            }
        }

        [Description("图标颜色")]
        [Category("SunnyUI")]
        [DefaultValue(typeof(Color), "White")]
        public Color ButtonSymbolColor
        {
            get
            {
                return btnAdd.SymbolColor;
            }
            set
            {
                UISymbolButton uISymbolButton = btnDec;
                Color symbolColor = (btnAdd.SymbolColor = value);
                uISymbolButton.SymbolColor = symbolColor;
            }
        }

        [Description("边框颜色")]
        [Category("SunnyUI")]
        [DefaultValue(typeof(Color), "80, 160, 255")]
        public Color ButtonRectColor
        {
            get
            {
                return btnAdd.RectColor;
            }
            set
            {
                UIPanel uIPanel = pnlValue;
                UISymbolButton uISymbolButton = btnDec;
                Color color2 = (btnAdd.RectColor = value);
                Color color4 = (uISymbolButton.RectColor = color2);
                uIPanel.RectColor = color4;
            }
        }

        [DefaultValue(29)]
        public int ButtonWidth
        {
            get
            {
                return buttonWidth;
            }
            set
            {
                buttonWidth = Math.Max(value, 29);
                if (btnAdd != null && btnDec != null)
                {
                    UISymbolButton uISymbolButton = btnAdd;
                    int width = (btnDec.Width = buttonWidth);
                    uISymbolButton.Width = width;
                }
            }
        }

        public override Color ForeColor
        {
            get
            {
                return pnlValue.ForeColor;
            }
            set
            {
                pnlValue.ForeColor = value;
            }
        }

        public event OnValueChanged ValueChanged;

        public CusUIUintUpDown()
        {
            InitializeComponent1();
            SetStyleFlags();
            base.ShowText = false;
            edit.Type = UITextBox.UIEditType.Double; //
            edit.Parent = pnlValue;
            edit.Visible = false;
            edit.BorderStyle = BorderStyle.None;
            edit.MouseLeave += Edit_Leave;
            pnlValue.Paint += PnlValue_Paint;
            btnAdd.Style = UIStyle.Custom;
            btnDec.Style = UIStyle.Custom;
        }

        public Control ExToolTipControl()
        {
            return pnlValue;
        }

        private void PnlValue_Paint(object sender, PaintEventArgs e)
        {
            if (base.Enabled)
            {
                e.Graphics.DrawLine(base.RectColor, 0, 0, pnlValue.Width, 0);
                e.Graphics.DrawLine(base.RectColor, 0, base.Height - 1, pnlValue.Width, base.Height - 1);
            }
            else
            {
                e.Graphics.DrawLine(base.RectDisableColor, 0, 0, pnlValue.Width, 0);
                e.Graphics.DrawLine(base.RectDisableColor, 0, base.Height - 1, pnlValue.Width, base.Height - 1);
            }
        }

        private void Edit_Leave(object sender, EventArgs e)
        {
            if (edit.Visible)
            {
                edit.Visible = false;
                pnlValue.FillColor = pnlColor;
                Value = (uint)edit.DoubleValue;
            }
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            if (DefaultFontSize < 0f && pnlValue != null)
            {
                pnlValue.Font = Font;
            }

            if (DefaultFontSize < 0f && edit != null)
            {
                edit.Font = Font;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!ReadOnly)
            {
                Value += (uint)Step;
                if (edit.Visible)
                {
                    edit.Visible = false;
                    pnlValue.FillColor = pnlColor;
                }
            }
        }

        private void btnDec_Click(object sender, EventArgs e)
        {
            if (!ReadOnly)
            {
                Value -= (uint)Step;
                if (edit.Visible)
                {
                    edit.Visible = false;
                    pnlValue.FillColor = pnlColor;
                }
            }
        }

        private void pnlValue_DoubleClick(object sender, EventArgs e)
        {
            if (!ReadOnly && Inputable)
            {
                edit.Left = 1;
                edit.Top = (pnlValue.Height - edit.Height) / 2;
                edit.Width = pnlValue.Width - 2;
                pnlColor = pnlValue.FillColor;
                pnlValue.FillColor = Color.White;
                edit.TextAlign = HorizontalAlignment.Center;
                //edit.IntValue = (int)Value; //
                edit.DoubleValue = (double)Value; //
                edit.BringToFront();
                edit.Visible = true;
                edit.Focus();
                edit.SelectAll();
            }
        }

        protected override void OnRadiusSidesChange()
        {
            if (btnDec != null && btnAdd != null)
            {
                btnDec.RadiusSides = (base.RadiusSides.HasFlag(UICornerRadiusSides.LeftTop) ? UICornerRadiusSides.LeftTop : UICornerRadiusSides.None) | (base.RadiusSides.HasFlag(UICornerRadiusSides.LeftBottom) ? UICornerRadiusSides.LeftBottom : UICornerRadiusSides.None);
                btnAdd.RadiusSides = (base.RadiusSides.HasFlag(UICornerRadiusSides.RightTop) ? UICornerRadiusSides.RightTop : UICornerRadiusSides.None) | (base.RadiusSides.HasFlag(UICornerRadiusSides.RightBottom) ? UICornerRadiusSides.RightBottom : UICornerRadiusSides.None);
            }
        }

        protected override void OnRadiusChanged(int value)
        {
            if (btnDec != null && btnAdd != null)
            {
                UISymbolButton uISymbolButton = btnDec;
                int num2 = (btnAdd.Radius = value);
                uISymbolButton.Radius = num2;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (base.Height < 20)
            {
                base.Height = 20;
            }

            if (base.Height > 60)
            {
                base.Height = 60;
            }
        }

        public override void SetStyleColor(UIBaseStyle uiColor)
        {
            base.SetStyleColor(uiColor);
            btnAdd.SetStyleColor(uiColor);
            btnDec.SetStyleColor(uiColor);
            pnlValue.SetStyleColor(uiColor);
            btnAdd.Invalidate();
            btnDec.Invalidate();
            pnlValue.Invalidate();
        }

        protected override void AfterSetFillColor(Color color)
        {
            base.AfterSetFillColor(color);
            if (pnlValue != null)
            {
                pnlValue.FillColor = color;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }

            base.Dispose(disposing);
        }

        private void InitializeComponent1()
        {
            this.btnDec = new Sunny.UI.UISymbolButton();
            this.btnAdd = new Sunny.UI.UISymbolButton();
            this.pnlValue = new Sunny.UI.UIPanel();
            base.SuspendLayout();
            this.btnDec.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnDec.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnDec.Font = new System.Drawing.Font("宋体", 12f);
            this.btnDec.ImageInterval = 1;
            this.btnDec.Location = new System.Drawing.Point(0, 0);
            this.btnDec.Margin = new System.Windows.Forms.Padding(0);
            this.btnDec.MinimumSize = new System.Drawing.Size(1, 1);
            this.btnDec.Name = "btnDec";
            this.btnDec.Padding = new System.Windows.Forms.Padding(26, 4, 0, 0);
            this.btnDec.RadiusSides = Sunny.UI.UICornerRadiusSides.LeftBottom | Sunny.UI.UICornerRadiusSides.LeftTop;
            this.btnDec.Size = new System.Drawing.Size(29, 29);
            this.btnDec.Symbol = 61544;
            this.btnDec.TabIndex = 0;
            this.btnDec.TipsText = null;
            this.btnDec.Click += new System.EventHandler(btnDec_Click);
            this.btnAdd.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnAdd.Dock = System.Windows.Forms.DockStyle.Right;
            this.btnAdd.Font = new System.Drawing.Font("宋体", 12f);
            this.btnAdd.ImageInterval = 1;
            this.btnAdd.Location = new System.Drawing.Point(87, 0);
            this.btnAdd.Margin = new System.Windows.Forms.Padding(0);
            this.btnAdd.MinimumSize = new System.Drawing.Size(1, 1);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Padding = new System.Windows.Forms.Padding(26, 3, 0, 0);
            this.btnAdd.RadiusSides = Sunny.UI.UICornerRadiusSides.RightBottom | Sunny.UI.UICornerRadiusSides.RightTop;
            this.btnAdd.Size = new System.Drawing.Size(29, 29);
            this.btnAdd.Symbol = 61543;
            this.btnAdd.TabIndex = 1;
            this.btnAdd.TipsText = null;
            this.btnAdd.Click += new System.EventHandler(btnAdd_Click);
            this.pnlValue.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnlValue.Font = new System.Drawing.Font("宋体", 12f);
            this.pnlValue.Location = new System.Drawing.Point(29, 0);
            this.pnlValue.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.pnlValue.MinimumSize = new System.Drawing.Size(1, 1);
            this.pnlValue.Name = "pnlValue";
            this.pnlValue.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
            this.pnlValue.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom | System.Windows.Forms.ToolStripStatusLabelBorderSides.Top;
            this.pnlValue.Size = new System.Drawing.Size(58, 29);
            this.pnlValue.TabIndex = 2;
            this.pnlValue.Text = "0";
            this.pnlValue.Click += new System.EventHandler(pnlValue_DoubleClick);
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            base.Controls.Add(this.pnlValue);
            base.Controls.Add(this.btnAdd);
            base.Controls.Add(this.btnDec);
            this.MinimumSize = new System.Drawing.Size(100, 0);
            base.Name = "UIIntegerUpDown";
            base.Size = new System.Drawing.Size(116, 29);
            base.ResumeLayout(false);
        }
    }
}

